﻿using ApiLib.RCL.Models;
using ApiLib.RCL.Utils;
using Microsoft.AspNetCore.Components.Forms;

namespace ApiLib.RCL.Components.AutoTable.Models;

/// <summary>
/// 基础自动表单实体对象，实现基础网络接口
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class BaseAutoTable<T>:IAutoTable<T> where T : IAutoTable<T>
{
    /// <summary>
    /// 获取请求地址，不带Host
    /// </summary>
    /// <returns></returns>
    protected abstract string GetDir();
    /// <summary>
    /// 获取请求头
    /// </summary>
    /// <param name="provider"></param>
    /// <returns></returns>
    protected abstract Task<Dictionary<string, string>> GetHeaders(IServiceProvider provider);
    
    private readonly IServiceProvider _provider=WebApp.ServiceProvider;
    /// <summary>
    /// 获取请求头
    /// </summary>
    /// <returns></returns>
    protected Task<Dictionary<string, string>> GetHeaders()
    {
        return GetHeaders(_provider);
    }

    public override async Task<BaseResponse<ListResponse<T>>?> OnQuery(Dictionary<string, object> form)
    {
        var headers = await GetHeaders();
        // return await HttpUtil.HttpPostAsync<BaseResponse<ListResponse<QyRzBankProduct>>>($"{GetHost()}{_dir}/List",headers, form);
        var res = await HttpUtil.HttpPostAsync<BaseResponse<ListResponse<T>>>($"{GetHost()}{GetDir()}/List", headers,
            form);
        return res;
    }

    public override async Task<BaseResponse<bool>?> OnSaveOrUpdate()
    {
        var headers = await GetHeaders();
        return await HttpUtil.HttpPostAsync<BaseResponse<bool>>($"{GetHost()}{GetDir()}/Save", headers, this);
    }

    public override async Task<BaseResponse<bool>?> OnDelete()
    {
        var headers = await GetHeaders();
        return await HttpUtil.HttpDeleteAsync<BaseResponse<bool>>($"{GetHost()}{GetDir()}/Delete/{Id}", headers);
    }

    public override async Task<Stream?> OnExportTemplate()
    {
        var headers = await GetHeaders();
        return await HttpUtil.DownloadAsync($"{GetHost()}{GetDir()}/ExportUploadModel", headers, true);
    }

    public override async Task<Stream?> OnExport(Dictionary<string, object> form)
    {
        var headers = await GetHeaders();
        return await HttpUtil.DownloadAsync($"{GetHost()}{GetDir()}/Export", headers, true, form);
    }

    public override async Task<BaseResponse<string>?> OnUpload(IBrowserFile file)
    {
        var headers = await GetHeaders();
        return await HttpUtil.UploadAsync<BaseResponse<string>>($"{GetHost()}/api/Common/Upload", file, headers);
    }

    public override async Task<BaseResponse<string>?> OnUpload(Stream stream,string fileName)
    {
        var headers = await GetHeaders();
        return await HttpUtil.UploadAsync<BaseResponse<string>>($"{GetHost()}/api/Common/Upload", stream,fileName, headers);
    }

    public override async Task<BaseResponse<bool>?> OnImport(IBrowserFile file)
    {
        var headers = await GetHeaders();
        return await HttpUtil.UploadAsync<BaseResponse<bool>>($"{GetHost()}{GetDir()}/Import", file, headers);
    }
}